/*
 *   MetrixViewer - A simple app for viewer Metrix data. Uses XML data from Webosroundup's Metrix data and graphs it using Google charts API.
 *   http://code.google.com/p/metrixviewer/
 *   Copyright (C) 2010  Wes Brown
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
function EditCustomAssistant() {

}

EditCustomAssistant.prototype.setup = function() {

	//Widgets
	this.custom1TextModel = {
             value: "",
             disabled: false
    };
	this.controller.setupWidget("custom1FieldId",
        {
            hintText: $L(""),
            multiline: false,
            enterSubmits: false,
            focus: false,
			textCase: Mojo.Widget.steModeLowerCase
         },
         this.custom1TextModel
    ); 
	
	this.custom2TextModel = {
             value: "",
             disabled: false
    };
	this.controller.setupWidget("custom2FieldId",
        {
            hintText: $L(""),
            multiline: false,
            enterSubmits: false,
            focus: false,
			textCase: Mojo.Widget.steModeLowerCase
         },
         this.custom2TextModel
    ); 
	
	this.custom3TextModel = {
             value: "",
             disabled: false
    };
	this.controller.setupWidget("custom3FieldId",
        {
            hintText: $L(""),
            multiline: false,
            enterSubmits: false,
            focus: false,
			textCase: Mojo.Widget.steModeLowerCase
         },
         this.custom3TextModel
    ); 
	
	this.custom4TextModel = {
             value: "",
             disabled: false
    };
	this.controller.setupWidget("custom4FieldId",
        {
            hintText: $L(""),
            multiline: false,
            enterSubmits: false,
            focus: false,
			textCase: Mojo.Widget.steModeLowerCase
         },
         this.custom4TextModel
    ); 
	
	this.controller.setupWidget("submitButtonId",
         {},
         {
             label : $L("Submit"),
             disabled: false
         }
     );
	
	Mojo.Event.listen(this.controller.get("submitButtonId"),Mojo.Event.tap, this.submit.bind(this));
};

EditCustomAssistant.prototype.activate = function(event) {

	$('scene-title').innerText = "Custom Counts: " + MetrixViewer.appname;
	$('frontendGroupLabel').innerText = MetrixViewer.packageId;
	
	this.custom1TextModel.value = getCustomMetric(MetrixViewer.customCookieObject,MetrixViewer.packageId,1);
	this.controller.modelChanged(this.custom1TextModel);
	
	this.custom2TextModel.value = getCustomMetric(MetrixViewer.customCookieObject,MetrixViewer.packageId,2);
	this.controller.modelChanged(this.custom2TextModel);
	
	this.custom3TextModel.value = getCustomMetric(MetrixViewer.customCookieObject,MetrixViewer.packageId,3);
	this.controller.modelChanged(this.custom3TextModel);
	
	this.custom4TextModel.value = getCustomMetric(MetrixViewer.customCookieObject,MetrixViewer.packageId,4);
	this.controller.modelChanged(this.custom4TextModel);

};

EditCustomAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	   this scene is popped or another scene is pushed on top */
};

EditCustomAssistant.prototype.cleanup = function(event) {
	/* this function should do any cleanup needed before the scene is destroyed as 
	   a result of being popped off the scene stack */
};





EditCustomAssistant.prototype.submit = function(event) {

	var newCustomList = addCustomMetric(MetrixViewer.customCookieObject,MetrixViewer.packageId,this.custom1TextModel.value,this.custom2TextModel.value,this.custom3TextModel.value,this.custom4TextModel.value);

	
	MetrixViewer.customCookieObject.clear();
	Object.extend(MetrixViewer.customCookieObject,newCustomList);
	MetrixViewer.customCookie.put(MetrixViewer.customCookieObject);
	
	
	Mojo.Log.info("New customer cookie is %j",MetrixViewer.customCookieObject);
	
	//Return to app selector
	Mojo.Controller.stageController.popScene();

};
